Completed
Push — master ( bf23bc...4f23d4 )
by Patrick
06:42
created

areas.js ➔ update_area   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
function area_change(control)
2
{
3
    var val = $(control).val();
4
    if(val != '')
5
    {
6
        $('#area_details').show();
7
        $('#submit').unbind('click');
8
9
        if(val == '_new')
10
        {
11
            $('#submit').on('click', add_area);
12
            $('#area_name').html("New Area");
13
        }
14
        else
15
        {
16
            $('#submit').on('click', update_area);
17
            $('#area_name').html(val);
18
            var area = $('#area_select :selected').data('area');
19
            $('#short_name').val(area.short_name);
20
            $('#name').val(area.name);
21
        }
22
    }
23
}
24
25
function areas_done(data)
26
{
27
    for(var i = 0; i < data.length; i++)
28
    {
29
        var opt = $('<option/>', {value: data[i].short_name}).html(data[i].name);
30
        opt.appendTo($('#area_select'));
31
        opt.data('area', data[i]);
32
    }
33
}
34
35
function areas_post_done()
36
{
37
    location.reload();
38
}
39
40
function form_vars()
41
{
42
    return {
43
        short_name: $('#short_name').val(),
44
        name: $('#name').val()
45
    };
46
}
47
48
function add_area()
49
{
50
    $.ajax({
51
        url: '../api/v1/areas',
52
        data: JSON.stringify(form_vars()),
53
        type: 'POST',
54
        processData: false,
55
        dataType: 'json',
56
        success: areas_post_done});
57
}
58
59
function update_area()
60
{
61
    $.ajax({
62
        url: '../api/v1/areas/'+$('#area_name').html(),
63
        data: JSON.stringify(form_vars()),
64
        type: 'PATCH',
65
        processData: false,
66
        dataType: 'json',
67
        success: areas_post_done});
68
}
69
70
function init_page()
71
{
72
    $.ajax({
73
        url: '../api/v1/areas',
74
        type: 'get',
75
        dataType: 'json',
76
        success: areas_done});
77
}
78
79
$(init_page);
80